<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class UserAssociation extends AuthBase
{
    //获取关联企业信息
    public function index()
    {
        $user = (new \app\common\business\UserAssociation())->getNormalSimpleInfoByField('user_id',$this->userId);
        //前端详情类接口都返回null
        if (empty($user)) $user = null;
        return Show::success($user, "OK");
    }

    //更新关联企业信息
    public function update(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $name = $this->request->param("name", "", "trim");
        $position = $this->request->param("position", "", "trim");
        $business_license = $this->request->param("business_license", "", "trim");

        //参数校验
        $data = [
            'name' => $name,
            'position' => $position,
            'business_license' => $business_license,
        ];
        $validate = new \app\api\validate\UserAssociation();
        if (!$validate->scene('update')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $userBisObj = new \app\common\business\UserAssociation();
        try {
            $user = $userBisObj->saveData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$user) {
            return Show::error([], "更新失败");
        }

        return Show::success([], "更新成功");
    }
}